import TLCam
import unittest
import os

class TLCamTest(unittest.TestCase):
    
    # === TLCam.createSession() =======================================

    def test_createSession_firstSession(self): 
        tlc = TLCam.getTLC()
        tlc.createSession(1234567)

        # exists the directory for the session?
        isSession = os.path.exists('./' + tlc.getPrefix() + str(1001))
        self.assertTrue(isSession, 'session directory does not exist')
    
        # exists the meta-file?
        metaPath = './' + tlc.getPrefix() + str(1001)+'/'+TLCam.TLC.META_FILE
        isMeta = os.path.exists(metaPath)
        self.assertTrue(isMeta, 'metafile does not exist')
        
        # contains the meta-file the timestamp
        f = open(metaPath, "r+")
        timestamp = int(f.readline())
        f.close()
        self.assertEquals(timestamp, 1234567, 'written timestamp is not equal to expected')

        self.test_tidyUp()
    
    # === TLCam.getSessions() =======================================

    def test_getSessions_getBeforeCreatedSession(self): 
        tlc = TLCam.getTLC()
        sessions = tlc.getSessions()

        # no session available
        self.assertEqual(len(sessions), 0, 'sessions list has not one element')
        
        tlc.createSession(1234567)
        sessions = tlc.getSessions()
        
        # is it the first session
        self.assertEqual(sessions[0].dirName, 'tlc_1001', 'session element is not tlc_1001')

        self.test_tidyUp()

    def test_getSessions_withoutSessions(self): 
        self.test_tidyUp()
        
        tlc = TLCam.getTLC()
        sessions = tlc.getSessions()
        self.assertTrue(sessions == [], 'sessions list is not empty')

    
    # === TLCam.getSession() =======================================

    def test_getSession_getBeforeCreatedSession(self):
        tlc = TLCam.getTLC()
        session = tlc.getSession('tlc_1002')
        self.assertIsNone(session, 'session is not none')

    def test_getSession_withNoneAndEmpty(self): 
        tlc = TLCam.getTLC()
        session = tlc.getSession(None)
        self.assertIsNone(session, 'session is not none')

        session = tlc.getSession('')
        self.assertIsNone(session, 'session is not none')

        
    # === TLCam.deleteSession() =======================================

    def test_deleteSession_withNullAndEmpty(self):
        tlc = TLCam.getTLC()
        self.assertRaises(Exception, tlc.deleteSession, None)
        self.assertRaises(Exception, tlc.deleteSession, '')

    def test_deleteSession_withNonExistingSession(self):
        tlc = TLCam.getTLC()
        self.assertRaises(Exception, tlc.deleteSession, 'tlc_1002')

    def test_deleteSession_withExistingSession(self):
        self.test_tidyUp()

        tlc = TLCam.getTLC()
        
        sessions = tlc.getSessions()
        self.assertTrue(sessions == [], 'sessions list is not empty')
        
        tlc.createSession(1234567)
        sessions = tlc.getSessions()
        self.assertEquals(len(sessions), 1, 'sessions list is not an element')
        
        self.test_tidyUp()

    
    # === Tidy up =======================================

    def test_tidyUp(self): 
        try: 
            os.remove('./tlc_1001/meta.tlc')
            os.rmdir('./tlc_1001')
        except:
            pass
        
    # ==============================================    
    

# ==============================================    
if __name__ == "__main__": 
    unittest.main()
    
